package com.buddies.lottery.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ConvertUtils;
import com.buddies.lottery.R;
import com.buddies.lottery.common.ConfigManager;

import java.util.Collections;
import java.util.List;

/**
 * Created by yong on 17/1/20.
 */

public class LotteryBetButton extends LinearLayout {

    private OnBetButtonClickListener mListener;

    public LotteryBetButton(Context context) {
        this(context, null);
    }

    public LotteryBetButton(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LotteryBetButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        // 读取配置中投注按钮数据
        List<Integer> buttons = ConfigManager.getInstance().getLotteryBetButtons();
        Collections.sort(buttons);

        for (final int betOptionId : buttons) {
            View button = View.inflate(getContext(), R.layout.item_lottery_bet_button, null);
            ((TextView)button.findViewById(R.id.btn_bet_num)).setText(String.valueOf(betOptionId));

            button.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mListener != null) {
                        mListener.onBetButtonClick(betOptionId);
                    }
                }
            });

            LayoutParams lps = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            lps.leftMargin = ConvertUtils.dp2px(4);
            button.setLayoutParams(lps);
            addView(button);
        }
    }

    public void setOnBetButtonClickListener(OnBetButtonClickListener listener) {
        mListener = listener;
    }

    public interface OnBetButtonClickListener {
        void onBetButtonClick(int betOptionId);
    }

}
